<template>
  <el-dialog
    :title="'新增'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="家庭组名称" prop="familyGroupName">
      <el-row>
        <el-input v-model="dataForm.familyGroupName" placeholder="家庭组名称">
          <el-button slot="append"  @click="getFamilyList(dataForm)">选择</el-button>
        </el-input>
      </el-row>
    </el-form-item>

    <el-table :data="dataList" border header-cell-class-name="miui-header-cell-style">
      <el-table-column prop="customerName" header-align="center" align="center"  label="会员名称"></el-table-column>
      <el-table-column prop="integral" header-align="center" align="center" label="医院积分"></el-table-column>
      <el-table-column prop="storesIntegral" header-align="center" align="center" label="门店积分"></el-table-column>
    </el-table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>
    <!-- 选择家庭组 -->
    <change-family v-if="changeVisible" ref="changeDialog" @refreshData="getFamilyInfo"></change-family>
  </el-dialog>
</template>

<script>
    import {addObj, getInfoByCustomerId,editInfoLink} from '@/api/eims/familygroup'
    import ChangeFamily from './changefamilygroup-form'
    import {mapGetters} from 'vuex'
    export default {
    data () {
      return {
        visible: false,
        ischeck: false,
        changeVisible: false,
        createVisible: false,
        dataForm: {
          id: 0,
          familyGroupName: '',
          customerId: '',
          unitId: '',
          tanentId: '',
          createUser: '',
          createTime: '',
          updateUser: '',
          updateName: ''
        },
        dataList: [],
        dataRule: {
          familyGroupName: [
            { required: true, message: '家庭组名称不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    components: {
      ChangeFamily
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    methods: {
      init (customerId) {
        for(let key in this.dataForm){
          if(key != 'links'){
            this.dataForm[key] = ''
          }else{
            this.dataForm['links'] = []
          }
        }
        this.dataForm.customerId = customerId || 0
        this.dataList = []
        this.visible = true
        this.$nextTick(() => {
          // this.$refs['dataForm'].resetFields()
          getInfoByCustomerId(this.dataForm.customerId,this.userInfo.lessDeptId).then(response => {
            if(response.data.code == '0'){
              this.dataForm = response.data.data
              this.dataForm.currentDeptId = this.userInfo.lessDeptId
              this.dataList = response.data.data.links
            }else{
              this.$message.error(response.data.msg)
            }
          })
        })
        this.dataForm.currentDeptId = this.userInfo.lessDeptId
      },
      // 表单提交
      dataFormSubmit () {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.familyId) {
                editInfoLink(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                  this.ischeck = false
                }).catch(() => {
                  this.ischeck = false
                })
            } else {
                addObj(this.dataForm).then(response => {
                    if(response.data.code == '0'){
                      this.$message.success('添加成功')
                      this.visible = false
                      this.$emit('refreshDataList')
                    }else{
                      this.$message.error(response.data.msg)
                    }
                    this.ischeck = false
                }).catch(() => {
                  this.ischeck = false
                })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      getFamilyList(data){
        this.changeVisible = true
        if(data.id){
          let info = {
            id : data.id,
            customerId : data.customerId
          }
          this.$nextTick(() => {
            this.$refs.changeDialog.init(info)
          })
        }else{
          let info = {
            customerId : data.customerId
          }
          this.$nextTick(() => {
            this.$refs.changeDialog.init(info)
          })
        }
        
      },
      getFamilyInfo(data){
        this.dataForm.familyGroupName = data.familyGroupName
        this.dataForm.familyId = data.id
      },
    }, 
  }
</script>
